package com.hxkcloud.patient.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.adapter.DrugListAdapter;
import com.hxkcloud.patient.beans.DrugBean;
import com.hxkcloud.patient.beans.DrugsList;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by william_sun on 2016-10-08.
 * Description:药品列表
 */
public class DrugListActivity extends BaseActivity implements View.OnClickListener {

    //title
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private RecyclerView rv;
    private RecyclerView rv_add;
   // private Button bt_next;

    private List<DrugBean> drugs;
    private List<DrugBean> drugs_add;
    private DrugListAdapter adapter;
    private DrugListAdapter adapter_add;
    private LinearLayout addnewdrug;
    private String patientId = "";
    private int intentType = 0;// 0 表示添加服药记录  1表示添加停药记录 2表示重选停药

    public static final int requestCode = 999;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_DRUGSLIST_SUCCESS:
                DrugsList drugsLists = (DrugsList) msg.obj;
                drugs = drugsLists.getList();
                adapter.setData(drugs);


                drugs_add = drugsLists.getListMedInfo();
               // adapter_add.notifyDataSetChanged();
              //  Log.i("--drugs-->","drugs_add:"+drugs_add.toString());
                adapter_add.setData(drugs_add);
                //更新新加的药物数据
              //  adapter_add.notifyDataSetChanged();
                break;
            case MessageCode.NETWORKCODE_DRUGSLIST_FAILURE:
                if ((msg.obj != null) && (!TextUtils.isEmpty((String) msg.obj))) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.datagetfail);
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_druglist);
        if(getIntent().hasExtra("patientId")){
            patientId = getIntent().getStringExtra("patientId");
        }
        if(getIntent().hasExtra("intentType"))
        {
            intentType = getIntent().getIntExtra("intentType",0);
        }

        initView();
        initData();
    }

    public static void jumpToDrugListActivity(Activity activity,String patientId,int intentType)
    {
        Intent iAdd = new Intent(activity, DrugListActivity.class);
        iAdd.putExtra("patientId",patientId);
        iAdd.putExtra("intentType",intentType);
        activity.startActivityForResult(iAdd,requestCode);
    }

    public static void jumpToDrugListActivity(Activity activity,String patientId,int intentType,int requestCode)
    {
        Intent iAdd = new Intent(activity, DrugListActivity.class);
        iAdd.putExtra("patientId",patientId);
        iAdd.putExtra("intentType",intentType);
        iAdd.putExtra("requestCode",requestCode);
        activity.startActivityForResult(iAdd,requestCode);
    }


    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.takedrugrecord));
        iv_back.setOnClickListener(this);

        rv = (RecyclerView) findViewById(R.id.rv_druglist);
        rv_add = (RecyclerView) findViewById(R.id.rv_druglist_add);
        // bt_next = (Button) findViewById(R.id.bt_druglist_next);
        addnewdrug = (LinearLayout) findViewById(R.id.druglist_addmedication);
        addnewdrug.setOnClickListener(this);
        // bt_next.setOnClickListener(this);
       // bt_next.setBackgroundResource(R.drawable.bt_grey);
       // bt_next.setEnabled(false);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(mLayoutManager);

        LinearLayoutManager addLayoutManager = new LinearLayoutManager(mContext);
        addLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_add.setLayoutManager(addLayoutManager);

        drugs = new ArrayList<>();
        drugs_add = new ArrayList<>();
        adapter = new DrugListAdapter(mContext, drugs, new DrugListAdapter.onDrugCheckListener() {
            @Override
            public void onDrugCheck(int position) {
               // drugs.get(position).setIsCheck(!drugs.get(position).isCheck());
              //  adapter.setData(drugs);

//                boolean isSelect = false;
//                for (DrugBean drug : drugs) {
//                    if (drug.isCheck()) {
//                        isSelect = true;
//                        break;
//                    }
//                }
//                if (isSelect) {
//                    bt_next.setBackgroundResource(R.drawable.bt_green);
//                    bt_next.setEnabled(true);
//                } else {
//                    bt_next.setBackgroundResource(R.drawable.bt_grey);
//                    bt_next.setEnabled(false);
//                }
                if(intentType == 0)
                {
                    DrugsList drugsList = new DrugsList();
                    List<DrugBean> drugBeans = new ArrayList<>();
                    drugBeans.add(drugs.get(position));
                    drugsList.setList(drugBeans);
                    Intent iAdd = new Intent(mContext, AddDrugActivity.class);
                    iAdd.putExtra("druglist",(Serializable) drugsList);
                    iAdd.putExtra("type",0);
                    startActivityForResult(iAdd,AddStopDrugActivity.requestCode);
                }else if(intentType == 1)
                {
                    //添加停药记录
                    AddStopDrugActivity.jumpToAddStopDrugActivity(DrugListActivity.this,drugs.get(position),0,null);
                }else
                {
                    Intent data = new Intent();
                    data.putExtra("bean",drugs.get(position));
                    setResult(Activity.RESULT_OK,data);
                    finish();
                }
                //从AddDrugActivity 回跳到服药记录页面
               // finish();
            }
        });
        adapter_add = new DrugListAdapter(mContext,drugs_add,new DrugListAdapter.onDrugCheckListener(){

            @Override
            public void onDrugCheck(int position)
            {
                if(intentType == 0)
                {
                    DrugsList drugsList_add = new DrugsList();
                    List<DrugBean> drugBeans_add = new ArrayList<>();
                    drugBeans_add.add(drugs_add.get(position));
                    drugsList_add.setListMedInfo(drugBeans_add);
                    Intent iAdds = new Intent(mContext, AddNewDrugUpdateActivity.class);
                    iAdds.putExtra("druglist",(Serializable) drugsList_add);
                    iAdds.putExtra("departmentId",loginInfo.getDepartmentId());
                    iAdds.putExtra("type",1);
                    startActivityForResult(iAdds,AddStopDrugActivity.requestCode);
                }else if(intentType == 1)
                {
                    //添加停药记录
                    AddStopDrugActivity.jumpToAddStopDrugActivity(DrugListActivity.this,drugs_add.get(position),0,null);
                }else
                {
                    Intent data = new Intent();
                    data.putExtra("bean",drugs_add.get(position));
                    setResult(Activity.RESULT_OK,data);
                    finish();
                }


               // finish();
            }
        });
        rv.setAdapter(adapter);
        rv_add.setAdapter(adapter_add);

        if(intentType == 0)
        {
            //添加
            addnewdrug.setVisibility(View.VISIBLE);
        }else
        {
            addnewdrug.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        loading();
        YiJiaHuNetWorkUtils.drugsList(loginInfo.getDepartmentId(),patientId, handler);
    }

    @Override
    public void onResume() {
        super.onResume();
        loading();
        YiJiaHuNetWorkUtils.drugsList(loginInfo.getDepartmentId(),patientId, handler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.druglist_addmedication:
                Intent addNewDrugIntent = new Intent(mContext,AddNewDrugActivity.class);
                   // addNewDrugIntent.putExtra()
//                addNewDrugIntent.putExtra("druglist",(Serializable) drugs);
                addNewDrugIntent.putExtra("departmentId",loginInfo.getDepartmentId());
                startActivity(addNewDrugIntent);
                //返回跳到第一个服药记录
              //  finish();
                break;
//            case R.id.bt_druglist_next:
//                DrugsList drugsList = new DrugsList();
//                List<DrugBean> drugBeans = new ArrayList<>();
//                for (DrugBean drug : drugs) {
//                    if (drug.isCheck()) {
//                        drugBeans.add(drug);
//                    }
//                }
//                if (drugBeans.size() == 0) {
//                    toastMsg(R.string.selectleastonedrug);
//                    return;
//                }
//                drugsList.setList(drugBeans);
//
//                Intent iAdd = new Intent(mContext, AddDrugActivity.class);
//                iAdd.putExtra("druglist", (Serializable) drugsList);
//                startActivity(iAdd);
//                finish();
//                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == AddStopDrugActivity.requestCode && resultCode == RESULT_OK)
        {
            Intent intent = new Intent();
            intent.putExtra("intentType",intentType);
            setResult(RESULT_OK,intent);
            finish();
        }
    }
}
